<?php
class ml_model_album_dbAlbumreply extends Lib_datamodel_db
{
    private $_status_array = array('ok'=>1,'del'=>9);
    private $_fields = array(
                'albumreply_rid',
                'uid',
                'content',
                'albumfeed_rid',
                'from',
                'status',
                'ctime',
                'cip',
            );
    
    public function __construct() {
        
        $db_config = ml_factory::load_standard_conf ( 'dbAlbum' );
        parent::__construct ( 'albumreply', $db_config ['albumreply'] );
        $this->_fields = '`'.implode('`,`', $this->_fields).'`';
    }
    
    public function set_fields($fields){
        if(empty($fields))
            return false;
        $this->_fields = is_array($fields) ? '`'.implode('`,`', $fields).'`' : $fields;
        return false;
    }
    
    /**
     * 通过albumreply_rid获取回复内容
     */
    public function get_by_albumreply_rid($rid){
        $album_id = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($album_id , self::DB_SLAVE))
            return false;
    
        $sql = "select ".$this->_fields." from `".$this->table."` where `albumreply_rid`='$rid'";
        $rs=$this->fetch_row($sql);
        return $rs;
    }
    /**
     * 通过albumfeed_rid获取回复内容列表
     */
    public function get_list_by_albumfeed_rid($rid, $begin, $count){
        $album_id = ml_tool_resid::resid2uid($rid);
        if(!$this->init_db($album_id , self::DB_SLAVE  ))
            return false;
        if($begin < 0)
            $begin = 0;
        $sql = "select  ".$this->_fields."  from  `$this->table`  where `albumfeed_rid`='$rid' and `status`='".$this->_status_array[ok]."'  ORDER BY ctime DESC  limit $begin,$count";
    
        $rs = $this->fetch($sql);
    
        return  $rs;
    
    }
    /**
     * 根据albumfeed_rid获取回复内容条数
     */
    public function get_count_by_albumfeed_rid($albumfeed_rid){
        $album_id = ml_tool_resid::resid2uid($albumfeed_rid);
        if(!$this->init_db($album_id , self::DB_SLAVE  ))
            return false;
        $where = "`albumfeed_rid`='$albumfeed_rid'  AND  `status`=".$this->_status_array['ok'];
        $rs = $this->fetch_count($where);
        return  $rs;        
    }

    /**
     * 插入评论内容
     */
    public function insert_content($data){
        $album_id = ml_tool_resid::resid2uid($data['albumreply_rid']);
        if(!$this->init_db($album_id , self::DB_MASTER ))
            return false;
        
        $array['albumreply_rid'] = $data['albumreply_rid'];
        $array['albumfeed_rid'] = $data['albumfeed_rid'];
        $array['uid'] = $data['uid'];
        $array['content'] = $data['content'];
        $array['from'] = empty($data['from']) ? ML_FROM_WEB : $data['from'];
        $array['status'] = $this->_status_array['ok'];
        $array['ctime'] = empty($data['ctime']) ? date('Y-m-d H:i:s') : $data['ctime'];
        $array['cip'] = empty($data['cip']) ? Tool_ip::get_real_ip() : $data['cip'];

        return $this->insert($array);
    }
    /**
     * 通过albumfeed_rid删除评论内容
     */
    public function del_by_albumreply_rid($albumreply_rid){
        $album_id = ml_tool_resid::resid2uid($albumreply_rid);
        if(!$this->init_db($album_id , self::DB_MASTER))
            return false;
            
        $data['status'] = $this->_status_array['del'];
        $where = "`albumreply_rid` = '$albumreply_rid'";
        return $this->update( $data, $where, 1);
    }
}
?>